package com.sumer.psychology.sharing.application.command;

import com.sumer.psychology.common.Assert;
import com.sumer.psychology.sharing.domain.model.*;
import com.sumer.psychology.sharing.domain.service.RecommendationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 分享的应用服务
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/9/10
 * Time: 10:24
 */
@Transactional
public class SharedResourceApplicationService {
    private SharedResourceRepository sharedResourceRepository;
    private RecommendationRepository recommendationRepository;
    private RecommendationService recommendationService;

    public SharedResourceApplicationService(SharedResourceRepository sharedResourceRepository,
                                            RecommendationRepository recommendationRepository,
                                            RecommendationService recommendationService) {
        this.sharedResourceRepository = sharedResourceRepository;
        this.recommendationRepository = recommendationRepository;
        this.recommendationService = recommendationService;
    }

    /**
     * 创建资源分享
     *
     * @param createSharedResourceCommand command
     */
    public SharedResourceId newSharedResource(CreateSharedResourceCommand createSharedResourceCommand) {
        SharedResource aSharedResource = SharedResourceFactory.instance().create(
                createSharedResourceCommand,
                sharedResourceRepository());

        sharedResourceRepository().add(aSharedResource);

        return aSharedResource.sharedResourceId();
    }

    /**
     * 修改资源分享
     *
     * @param changeSharedResourceCommand command
     */
    public void changeResource(ChangeSharedResourceCommand changeSharedResourceCommand) {
        SharedResource sharedResource = sharedResourceRepository().shareResourceOfId(
                new SharedResourceId(changeSharedResourceCommand.sharedResourceId()));

        Assert.notNull(sharedResource, "资源分享不能为空");

        sharedResource.change(
                changeSharedResourceCommand.topic(),
                changeSharedResourceCommand.sharedFiles());

        sharedResource.replaceTags(changeSharedResourceCommand.tags());
    }

    /**
     * 删除资源分享
     *
     * @param deleteSharedResourceCommand command
     */
    public void deleteResource(DeleteSharedResourceCommand deleteSharedResourceCommand) {
        SharedResource sharedResource = sharedResourceRepository().shareResourceOfId(
                new SharedResourceId(deleteSharedResourceCommand.deletedId()));

        if (sharedResource != null) {
            sharedResource.delete();
        }
    }

    /**
     * 推荐资源给好友
     *
     * @param recommendResourceCommand command
     */
    public void recommend(RecommendResourceCommand recommendResourceCommand) {
        SharedResource sharedResource = sharedResourceRepository().shareResourceOfId(
                new SharedResourceId(recommendResourceCommand.sharedResourceId()));

        Assert.notNull(sharedResource, "资源分享不能为空");

        Recommendation recommendation = recommendationService().recommend(
                sharedResource,
                recommendResourceCommand.friendId());
        recommendationRepository().add(recommendation);

    }

    private SharedResourceRepository sharedResourceRepository() {
        return sharedResourceRepository;
    }

    private RecommendationRepository recommendationRepository() {
        return recommendationRepository;
    }

    private RecommendationService recommendationService() {
        return recommendationService;
    }

}
